<?php

declare(strict_types=1);

namespace App\Admin;

use App\Library\Admin\MyAdmin;
use Presta\ImageBundle\Form\Type\ImageType;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Object\Metadata;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Show\ShowMapper;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;

final class InfoAdmin extends MyAdmin
{
    protected $accessMapping = ['log' => 'LOG'];

    protected $uploaderHelper;

    public function __construct($code, $class, $baseControllerName, UploaderHelper $uploaderHelper)
    {
        parent::__construct($code, $class, $baseControllerName);

        $this->uploaderHelper = $uploaderHelper;
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
    {
        $datagridMapper
            ->add('tag')
            ->add('title')
            ->add('version')
            ->add('isCurrent')
            ->add('isAutoUpdate')
            ->add('isShowDoc');
    }

    protected function configureListFields(ListMapper $listMapper): void
    {
        $listMapper
            ->add('id')
            ->addIdentifier('title')
            ->add('tag')
            ->add('version')
            ->add('isAutoUpdate')
            ->add('isShowDoc')
            ->add('description')
            ->add('_action', null, array(
                'actions' => array(
                    'edit' => [],
                    'log' => [
                        'template' => 'Backend/info/list__action_log.html.twig'
                    ],
                )
            ));
    }

    protected function configureFormFields(FormMapper $formMapper): void
    {
        if ($this->isCurrentRoute('create')) {
            $tag_options = [
                'help' => '创建后将无法修改'
            ];
        } else {
            $tag_options = [
                'disabled' => true,
                'required' => false,
            ];
        }

        $formMapper
            ->add('title')
            ->add('tag', null, $tag_options)
            ->add('logo_file', ImageType::class, [
                'enable_remote' => false,
                'max_width' => 800,
                'max_height' => 800,
                'preview_width' => '300px',
                'preview_height' => '300px',
                'upload_mimetype' => 'image/png',
                'required' => false,
                'delete_label' => '删除图片',
                'help' => '图片格式：jpg、png'
            ])
            ->add('description')
            ->add('isAutoUpdate', null, ['help' => '勾选后将自动更新API文档'])
            ->add('isShowDoc', null, ['help' => '勾选后将会自动展开文档']);
    }

    protected function configureShowFields(ShowMapper $showMapper): void
    {
        $showMapper
            ->add('id')
            ->add('title')
            ->add('tag')
            ->add('version')
            ->add('description')
            ->add('isAutoUpdate')
            ->add('isShowDoc');
    }

    protected function configureRoutes(RouteCollection $collection)
    {
        parent::configureRoutes($collection); // TODO: Change the autogenerated stub

        $collection->add('log', $this->getRouterIdParameter() . '/log');
        $collection->remove('delete');
        $collection->remove('export');
        $collection->remove('show');
    }

    public function prePersist($object)
    {
        parent::prePersist($object); // TODO: Change the autogenerated stub

        $object->setCreatedAt(new \DateTime());
    }

    public function configureActionButtons($action, $object = null)
    {
        $list = [];

        if (\in_array($action, ['tree', 'show', 'edit', 'delete', 'list', 'batch', 'log'], true)
            && $this->hasAccess('create')
            && $this->hasRoute('create')
        ) {
            $list['create'] = [
                // NEXT_MAJOR: Remove this line and use commented line below it instead
                'template' => $this->getTemplate('button_create'),
                //'template' => $this->getTemplateRegistry()->getTemplate('button_create'),
            ];
        }

        if (\in_array($action, ['show', 'delete', 'acl', 'history', 'log'], true)
            && $this->canAccessObject('edit', $object)
            && $this->hasRoute('edit')
        ) {
            $list['edit'] = [
                // NEXT_MAJOR: Remove this line and use commented line below it instead
                'template' => $this->getTemplate('button_edit'),
                //'template' => $this->getTemplateRegistry()->getTemplate('button_edit'),
            ];
        }

        if (\in_array($action, ['show', 'edit', 'acl', 'log'], true)
            && $this->canAccessObject('history', $object)
            && $this->hasRoute('history')
        ) {
            $list['history'] = [
                // NEXT_MAJOR: Remove this line and use commented line below it instead
                'template' => $this->getTemplate('button_history'),
                // 'template' => $this->getTemplateRegistry()->getTemplate('button_history'),
            ];
        }

        if (\in_array($action, ['edit', 'history', 'log'], true)
            && $this->isAclEnabled()
            && $this->canAccessObject('acl', $object)
            && $this->hasRoute('acl')
        ) {
            $list['acl'] = [
                // NEXT_MAJOR: Remove this line and use commented line below it instead
                'template' => $this->getTemplate('button_acl'),
                // 'template' => $this->getTemplateRegistry()->getTemplate('button_acl'),
            ];
        }

        if (\in_array($action, ['edit', 'history', 'acl', 'log'], true)
            && $this->canAccessObject('show', $object)
            && \count($this->getShow()) > 0
            && $this->hasRoute('show')
        ) {
            $list['show'] = [
                // NEXT_MAJOR: Remove this line and use commented line below it instead
                'template' => $this->getTemplate('button_show'),
                // 'template' => $this->getTemplateRegistry()->getTemplate('button_show'),
            ];
        }

        if (\in_array($action, ['edit', 'show'], true)
            && $this->canAccessObject('log', $object)
            && $this->hasRoute('log')
        ) {
            $list['log'] = [
                'template' => 'Backend/info/log_button.html.twig',
            ];
        }

        if (\in_array($action, ['show', 'edit', 'delete', 'acl', 'batch', 'log'], true)
            && $this->hasAccess('list')
            && $this->hasRoute('list')
        ) {
            $list['list'] = [
                // NEXT_MAJOR: Remove this line and use commented line below it instead
                'template' => $this->getTemplate('button_list'),
                // 'template' => $this->getTemplateRegistry()->getTemplate('button_list'),
            ];
        }

        return $list;
    }

    public function getTemplate($name)
    {
        switch ($name) {
            case 'edit':
                return 'Backend/info/edit.html.twig';
                break;
            default:
                return parent::getTemplate($name);
                break;
        }
    }

    public function getObjectMetadata($object)
    {
        $url = $this->uploaderHelper->asset($object, 'logo_file');

        return new Metadata($object->getTitle(), $object->getDescription(), $url);
    }
}
